package com.suixun.service.information.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suixun.dao.JfArticleGoodsArchivesMapper;
import com.suixun.dao.JfGoodsArchivesMapper;
import com.suixun.dao.ZxArticleCommentMapper;
import com.suixun.dao.ZxArticleMapper;
import com.suixun.dao.ZxArticleTagMapper;
import com.suixun.entity.JfArticleGoodsArchives;
import com.suixun.entity.JfArticleGoodsArchivesExample;
import com.suixun.entity.JfGoodsArchives;
import com.suixun.entity.JfGoodsArchivesExample;
import com.suixun.entity.ZxArticle;
import com.suixun.entity.ZxArticleComment;
import com.suixun.entity.ZxArticleTag;
import com.suixun.entity.ZxArticleTagExample;
import com.suixun.entity.ext.ExtZxArticleComment;
import com.suixun.service.credits.PutGoodsService;
import com.suixun.service.information.ZxArticleService;
import com.suixun.service.information.ZxArticleZanService;

import rop.thirdparty.org.apache.commons.lang3.StringUtils;

@Service
public class ZxArticleServiceImpl implements ZxArticleService {

	@Resource
	private SqlSessionTemplate sqlSessionTemplate;
	@Resource
	private ZxArticleMapper zxArticleMapper;
	@Resource
	private ZxArticleTagMapper zxArticleTagMapper;
	@Resource
	private ZxArticleZanService zxArticleZanService;
	@Resource
	private ZxArticleCommentMapper zxArticleCommentMapper;
	@Resource
	private PutGoodsService putGoodsService;

	@Override
	public PageInfo<ZxArticle> listArticleByTag(Integer page, Integer size, Integer tagId, String orderBy) {

		PageHelper.startPage(page, size);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tagId", tagId);
		map.put("orderBy", orderBy);
		List<ZxArticle> list = null;
		if (tagId == null) {
			list = sqlSessionTemplate.selectList("ZxArticleExtendSql.listArticle", map);
		} else {
			list = sqlSessionTemplate.selectList("ZxArticleExtendSql.listArticleByTag", map);
		}

		return new PageInfo<ZxArticle>(list);
	}

	@Override
	public ZxArticle getArticle(Integer articleId) {

		return sqlSessionTemplate.selectOne("ZxArticleExtendSql.getArticle", articleId);

	}

	@Override
	@Transactional
	public ZxArticle addArticle(ZxArticle article, Integer tagId) {
		Date date = new Date();
		article.setCreateTime(date);
		zxArticleMapper.insertSelective(article);

		ZxArticleTag artTag = new ZxArticleTag();
		artTag.setArticleId(article.getId());
		artTag.setTagId(tagId);
		artTag.setCreateTime(date);
		zxArticleTagMapper.insertSelective(artTag);

		return article;
	}

	@Override
	public void delArticle(Integer articleId) {

		ZxArticle article = zxArticleMapper.selectByPrimaryKey(articleId);
		if (article != null) {
			article.setDel((short) 1);
			zxArticleMapper.updateByPrimaryKeySelective(article);
		}
	}

	@Override
	public void updateArticle(ZxArticle article) {

		zxArticleMapper.updateByPrimaryKeySelective(article);

	}

	@Override
	public void setReadCount(Integer articleId, Integer count) {

		ZxArticle art = getArticle(articleId);
		if (art == null) {
			throw new RuntimeException("该文章不存在或已被删除");
		}
		art.setReadCount(count);
		zxArticleMapper.updateByPrimaryKeySelective(art);
	}

	@Override
	public void setZanCount(Integer articleId, Integer count) {

		ZxArticle art = getArticle(articleId);
		if (art == null) {
			throw new RuntimeException("该文章不存在或已被删除");
		}
		art.setZanCount(count);
		zxArticleMapper.updateByPrimaryKeySelective(art);
	}

	@Override
	public void setUp(Integer articleId) {

		ZxArticle art = getArticle(articleId);
		if (art == null) {
			throw new RuntimeException("该文章不存在或已被删除");
		}
		art.setUp(Integer.valueOf((int) (System.currentTimeMillis() / 1000)));
		zxArticleMapper.updateByPrimaryKeySelective(art);
	}

	@Override
	public PageInfo<ZxArticle> listArticleByNoTag(Integer page, Integer size) {

		PageHelper.startPage(page, size);
		List<ZxArticle> list = sqlSessionTemplate.selectList("ZxArticleExtendSql.listArticleByNoTag");

		return new PageInfo<ZxArticle>(list);
	}

	@Override
	public void changeTag(Integer articleId, Integer tagId) {

		ZxArticleTagExample example = new ZxArticleTagExample();
		example.createCriteria().andArticleIdEqualTo(articleId);

		ZxArticleTag zTag = new ZxArticleTag();
		zTag.setArticleId(articleId);
		zTag.setTagId(tagId);

		zxArticleTagMapper.updateByExampleSelective(zTag, example);
	}

	/**
	 * <p>
	 * Title: getArticleCommentPage
	 * </p>
	 * <p>
	 * 功能描述: 分页查询资讯评论回复信息
	 * </p>
	 * 
	 * @param page
	 * @param size
	 * @param articleId
	 * @param content
	 * @param userName
	 * @return
	 */
	@Override
	public PageInfo<ExtZxArticleComment> getArticleCommentPage(Integer page, Integer size, Integer articleId,
			String content, String userName) {
		PageHelper.startPage(page, size);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("articleId", articleId);
		map.put("content", content);
		map.put("userName", userName);
		List<ExtZxArticleComment> list = sqlSessionTemplate
				.selectList("ZxArticleCommentSql.listArticleCommentByActicle", map);
		return new PageInfo<ExtZxArticleComment>(list);
	}

	/**
	 * <p>
	 * Title: deleteArticleComment
	 * </p>
	 * <p>
	 * 功能描述: 删除资讯评论回复,多个用|隔开
	 * </p>
	 * 
	 * @param commentIds
	 * @return
	 */
	@Override
	@Transactional
	public void deleteArticleComments(String commentIds) {
		if (StringUtils.isBlank(commentIds)) {
			throw new RuntimeException("必要参数为空");
		}
		String[] commentIdArr = commentIds.split("[|]");
		ZxArticleComment zxArticleComment = null;
		for (String commentId : commentIdArr) {
			zxArticleComment = getArticleComment(Integer.parseInt(commentId));
			if (zxArticleComment == null) {
				throw new RuntimeException("该评论不存在或已删除");
			}
			deleteArticleComment(Integer.parseInt(commentId));
		}
	}

	private void deleteArticleComment(Integer commentId) {
		ZxArticleComment zxArticleComment = new ZxArticleComment();
		zxArticleComment.setId(commentId);
		zxArticleComment.setIsdeleted(true);
		zxArticleComment.setUpdateTime(new Date());
		zxArticleCommentMapper.updateByPrimaryKeySelective(zxArticleComment);
	}

	private ZxArticleComment getArticleComment(Integer commentId) {
		if (commentId != null) {
			ZxArticleComment zxArticleComment = zxArticleCommentMapper.selectByPrimaryKey(commentId);
			if (zxArticleComment == null || zxArticleComment.getIsdeleted()) {
				return null;
			} else {
				return zxArticleComment;
			}
		} else {
			return null;
		}
	}

	@Override
	@Transactional
	public ZxArticle addArticle(ZxArticle article, Integer tagId, String goodsNumbers) {
		Date date = new Date();
		article.setCreateTime(date);
		zxArticleMapper.insertSelective(article);

		ZxArticleTag artTag = new ZxArticleTag();
		artTag.setArticleId(article.getId());
		artTag.setTagId(tagId);
		artTag.setCreateTime(date);
		zxArticleTagMapper.insertSelective(artTag);
		// 若传入商品参数，则新增商品
		putGoodsService.addArticleGoodsArchives(article.getId(), goodsNumbers);
		return article;
	}

	@Override
	@Transactional
	public void updateArticle(ZxArticle article, String goodsNumbers) {
		zxArticleMapper.updateByPrimaryKeySelective(article);
		// 修改文章优惠券
		putGoodsService.updateArticleGoodsArchives(article.getId(), goodsNumbers);
	}
}
